package org.kunteng.yunac.lttask.controller.view;

import org.kunteng.yunac.lttask.AbstractTask;
import org.kunteng.yunac.lttask.LTTaskManager;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hedingwei on 6/5/15.
 */
@Controller
@RequestMapping(value = "${adminPath}/f/lttask")
public class Status {

    private ModelAndView success = new ModelAndView("f/lttask/status");

    @Autowired
    private LTTaskManager longTimeTaskRunner;

    @RequestMapping(value = "status.view.do")
    public ModelAndView action(HttpServletRequest request,@RequestParam("taskId")String taskId) {

        ModelAndView init = new ModelAndView("redirect:init.view.do?taskId="+taskId);

        AbstractTask task = (AbstractTask) longTimeTaskRunner.getRunningTask(taskId);
        if(task!=null){
            if(task.getProgress()!=100){
                success.addObject("isRunning",true);
                init.addObject("task_model",task.getModel());
            }else{
                success.addObject("isRunning",false);
                init.addObject("task_model",task.getModel());
            }
        }else{
            return init;
        }
        success.addObject("task_model",task.getModel());
        return success;
    }


    @RequestMapping(value = "status1.view.do")
    public ModelAndView action1(HttpServletRequest request,@RequestParam("taskId")String taskId) {

        System.out.println("第四次进来");
        ModelAndView init = new ModelAndView("redirect:${adminPath}/f/sd/detectlog/synch/init1.view.do?taskId="+taskId);
        AbstractTask task = (AbstractTask) longTimeTaskRunner.getRunningTask(taskId);
        if(task!=null){
            if(task.getProgress()!=100){
                success.addObject("isRunning",true);
                init.addObject("task_model",task.getModel());
            }else{
                success.addObject("isRunning",false);
                init.addObject("task_model",task.getModel());
            }
        }else{
            return init;
        }
        success.addObject("task_model",task.getModel());
        return success;
    }

    @RequestMapping(value = "status.json.do")
    @ResponseBody
    public JSONObject action2(HttpServletRequest request,@RequestParam("taskId")String taskId) {

        AbstractTask task = (AbstractTask) longTimeTaskRunner.getRunningTask(taskId);
        JSONObject ret = new JSONObject();
        if(task!=null){
            ret.put("hasTask",true);
            if(task.getProgress()!=100){
                ret.put("isRunning", true);
                ret.put("task_model", JSONObject.fromObject(task.getModel()));
            }else{
                ret.put("isRunning", false);
                ret.put("task_model", JSONObject.fromObject(task.getModel()));
            }
        }else{
            ret.put("hasTask",false);
        }
        return ret;
    }
}
